package com.amphora.web.actions;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.amphora.model.Client;
import com.amphora.services.application.ClientServices;
import com.amphora.services.application.impl.ClientServiceImpl;
import com.amphora.util.repository.JPAUtil;

@WebServlet("/RegisterAction")
public class RegisterAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static String REGISTER_SUCCESS = "/amphora_home.jsp";
	private static String REGISTER_ERROR_= "/client_register.jsp";
	private static String REGISTER_VIEW= "/client_register.jsp";
	private static String ERROR_ = "/error.jsp";
	private ClientServices cs;
	
	public void init(ServletConfig config) throws ServletException {
		System.out.println("RegisterAction init method");
		cs = new ClientServiceImpl();
	}

	public void destroy() {
		System.out.println("RegisterAction destroy method");
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("RegisterAction get method");
		
		Map parameters = request.getParameterMap();
		String forward = "";
		
		if(parameters.containsKey("view")){
			System.out.println("RegisterAction: Register selected");
			forward = REGISTER_VIEW;
		}else{
			System.out.println("RegisterAction: Register page error while loading");
			forward = ERROR_;
		}
		
		RequestDispatcher view = request.getRequestDispatcher(forward);
		view.forward(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("RegisterAction post method");
		HttpSession session = request.getSession();
		
		Map parameters = request.getParameterMap();
		String forward = "";

		if(parameters.containsKey("proceed")){
			HashMap<String, String> clientDetails = new HashMap<String,String>();
			clientDetails.put("username", request.getParameter("username"));
			clientDetails.put("password", request.getParameter("password"));
			clientDetails.put("name", request.getParameter("name"));
			clientDetails.put("surname", request.getParameter("surname"));
			clientDetails.put("contact", request.getParameter("contact"));
			clientDetails.put("email", request.getParameter("email"));
			clientDetails.put("cpassword",request.getParameter("cpassword"));
			
			if(clientDetails.get("name").equals("") || clientDetails.get("name")==null){
				request.setAttribute("message", "name cant be blank");
				forward = REGISTER_ERROR_;
			}
			else if(clientDetails.get("surname").equals("") || clientDetails.get("surname")==null){
				request.setAttribute("message", "surname cant be blank");
				forward = REGISTER_ERROR_;
			}
			else if(clientDetails.get("email").equals("") || clientDetails.get("email")==null){
				request.setAttribute("message", "email cant be blank");
				forward = REGISTER_ERROR_;
			}
			else if(JPAUtil.findByQuery("select c from "+Client.getClassName()+" c where c.email = '"+clientDetails.get("email")+"'").size()>0){
				request.setAttribute("message", "RegisterAction: Register user failed-> email already exists");
				forward = REGISTER_ERROR_;
			}else if(clientDetails.get("contact").equals("") || clientDetails.get("contact")==null){
				request.setAttribute("message", "contact cant be blank");
				forward = REGISTER_ERROR_;
			}
			else if(clientDetails.get("username").equals("") || clientDetails.get("username")==null){
				request.setAttribute("message", "username cant be blank");
				forward = REGISTER_ERROR_;
			}
			else if(clientDetails.get("password").equals("") || clientDetails.get("password")==null){
				request.setAttribute("message", "password cant be blank");
				forward = REGISTER_ERROR_;
			}
			else if(clientDetails.get("cpassword").equals("") || clientDetails.get("cpassword")==null){
				request.setAttribute("message", "confirm password cant be blank");
				forward = REGISTER_ERROR_;
			}
			else if(!clientDetails.get("cpassword").equals(clientDetails.get("password"))){
				request.setAttribute("message", "please ensure passwords match");
				forward = REGISTER_ERROR_;
			}
			else{
				if(JPAUtil.findByQuery("select c from "+Client.getClassName()+" c where c.username = '"+clientDetails.get("username")+"'").size()>0){
					System.out.println("RegisterAction: Register user failed-> username already exists");
					request.setAttribute("message", "RegisterAction: Register user failed-> username already exists");
					forward = REGISTER_ERROR_;
				}else{
					Long registeredSuccessfull = cs.registerClient(clientDetails);
					if(!registeredSuccessfull.equals(new Long(0))){
						System.out.println("RegisterAction: Register user success");
						session.setAttribute("loginID", registeredSuccessfull);
						request.setAttribute("message", "success");
						forward = REGISTER_SUCCESS;
					}else{
						System.out.println("RegisterAction: Register user failed-> error while creating user");
						forward = REGISTER_ERROR_;
					}
				}
			}
		}else{
			System.out.println("RegisterAction: Register page error while loading");
			forward = ERROR_;
		}
		
		RequestDispatcher view = request.getRequestDispatcher(forward);
		view.forward(request,response);
	}

}
